package com.ruleengine.router.impl;

import java.util.Iterator;

import org.apache.log4j.Logger;

import com.ruleengine.container.WorkflowContainerManager;
import com.ruleengine.context.IContextProperty;
import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.context.impl.WorkflowContextFactory;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ruleengine.frame.callback.IRuleEngineCallback;
import com.ruleengine.frame.exception.RuleEngineFrameException;
import com.ruleengine.frame.impl.WorkflowExecuter;
import com.ruleengine.router.exception.RouterException;

/**
 * 本地路由工作流调用。
 * 本地路由是默认工作流路由器
 * @author AB033862
 * Aug 23, 2012
 */
public class LocalRouter extends AbstractRouter {
    private static Logger logger = Logger.getLogger(LocalRouter.class);

    public void route(final String target, final IWorkflowConext context,
            final IEndpointData data) throws RouterException {
        if(WorkflowContainerManager.getInstance().getWorkflowTask(target)==null){
            //本地没有改工作流配置
            if(logger.isDebugEnabled()){
                logger.debug("can't find router information on local for task ["+target+"]");
            }
            //调用下一个路由器处理
            getRouter().route(target, context, data);
            return;
        }
        
        //在本地能够找到该工作流
        // 创建工作流context
        IWorkflowConext newContext = WorkflowContextFactory.getInstance()
                .createWorkflowContext(target);

        if(logger.isDebugEnabled()){
            logger.debug("copy public propertys from original context...");
        }
        // 拷贝public参数
        newContext.combination(context);

        try {
            WorkflowExecuter.getInstance().executeNonBlocking(newContext, data,
                    new IRuleEngineCallback() {
                        private IWorkflowConext oriContext = context;
                        private String subTarget = target;

                        public void onException(IWorkflowConext context,
                                Exception e) {
                            // TODO Auto-generated method stub
                            logger.debug("excute sub workflow ["
                                    + oriContext.getWorkflowId() + "-->"
                                    + subTarget + "] on exception");
                            // 设置异常
                            oriContext.setExceptionCause(e);

                            WorkflowExecuter.getInstance().executeCallBack(
                                    oriContext, data);
                        }

                        public void onComplete(IWorkflowConext context,
                                IEndpointData data1) {
                            // TODO Auto-generated method stub
                            logger.debug("excute sub workflow ["
                                    + oriContext.getWorkflowId() + "-->"
                                    + subTarget + "] on complete");
                            // 合并public属性
                            oriContext.combination(context);
                            
                            // 设置响应值
                            WorkflowExecuter.getInstance().executeCallBack(
                                    oriContext, data);

                        }

                    });
        } catch (RuleEngineFrameException e) {
            // TODO Auto-generated catch block
            throw new RouterException("route workflow task [" + target
                    + "] error", e);
        }
    }

}
